package org.jeecg.modules.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.demo.weixin.api.WeixinHelloApi;
import org.jeecg.modules.demo.weixin.dto.SpiltOrderDto;
import org.quartz.*;

import javax.annotation.Resource;

/**
 * 示例不带参定时任务
 * 
 * @Author Scott
 */
@Slf4j
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class SampleSplitJob implements Job {

	@Resource
	private WeixinHelloApi weixinHelloApi;
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		log.info("开始----------");
		SpiltOrderDto spiltOrderDto = new SpiltOrderDto();
		spiltOrderDto.setMerichantId(null);
		spiltOrderDto.setRecordId(null);

		UserTokenContext.setToken(getTemporaryToken());
		weixinHelloApi.splitOrder(spiltOrderDto);
		log.info("----结束");
		UserTokenContext.remove();
	}

	public static String getTemporaryToken() {
		RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
		//模拟登录生成临时Token
		//参数说明：第一个参数是用户名、第二个参数是密码的加密串
		String token = JwtUtil.sign("admin", "123456");
		// 设置Token缓存有效时间为 5 分钟
		redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
		redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, 5 * 60 * 1000);
		return token;
	}
}
